<?php

namespace app\admin\service\advertiser;

use app\admin\model\advertiser\AdvertiserSecondary as AdvertiserPage;
use app\admin\validates\AdvertiserSecondaryValidate as AdvertiserPageValidate;
use app\BaseService;
use think\exception\ValidateException;
use think\response\Json;

class AdvertiserSecondaryService extends BaseService
{
    ######################################## 页面广告位管理 ##############################################
    /**
     * 页面广告位列表
     * @param array $params
     * @return Json
     */
    public function pageList(array $params): Json
    {
        $where = [];
        //搜索展示位名称
        if ($params['name']) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }
        //搜索状态
        if ($params['status'] !== null and in_array($params['status'], [0, 1])) {
            $where[] = ['status', '=', $params['status']];
        }
        //获取列表
        $data = AdvertiserPage::getList($where, $params['page'], $params['limit'], 'id desc');
        return jsonReturn(1, $data);
    }

    /**
     * 页面广告位添加
     * @param array $params
     * @return Json
     */
    public function pageAdd(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('add')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //处理参数
        $params['create_time'] = time();
        $params['update_time'] = time();
        //写入数据库
        $id = AdvertiserPage::insertGetId($params);
        if ($id) {
            return jsonReturn(1, ['id' => $id], '添加成功');
        }
        return jsonReturn(0, [], '添加失败');
    }

    /**
     * 页面广告位编辑
     * @param array $params
     * @return Json
     */
    public function pageEdit(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('edit')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = AdvertiserPage::where(['id' => $params['id']])->find();
        if (!$Model) {
            return jsonReturn(0, [], '不存在');
        }
        //处理参数
        $params['update_time'] = time();
        //写入数据库
        $id = $Model->save($params);
        if ($id) {
            return jsonReturn(1, [], '编辑成功');
        }
        return jsonReturn(0, [], '编辑失败');
    }

    /**
     * 页面广告位删除
     * @param array $params
     * @return Json
     */
    public function pageDel(array $params): Json
    {
        //数据验证
        try {
            validate(AdvertiserPageValidate::class)->scene('del')->check($params);
        } catch (ValidateException $e) {
            return jsonReturn(0, [], $e->getError());
        }
        //获取当前模型
        $Model = AdvertiserPage::findOne($params);
        if (!$Model) {
            return jsonReturn(0, [], '不存在');
        }
        $res = AdvertiserPage::deleteData($params);
        if ($res) {
            return jsonReturn(1, [], '删除成功');
        }
        return jsonReturn(0, [], '删除失败');
    }
}